package edu.ped.android.bluetooth;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import edu.ped.android.nfc.TicTacToeServerActivity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothSocket;
import android.os.Handler;
import android.util.Log;

public class ConnectedThread extends Thread{

	BluetoothAdapter mBluetoothAdapter;
	private DataInputStream mdis;
	private DataOutputStream mdos;

	private int x;
	private int y;
	private Handler handler;

	public ConnectedThread(BluetoothSocket socket,Handler handler) {
		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		this.handler = handler;
		try {
			mdis = new DataInputStream(socket.getInputStream());
			mdos = new DataOutputStream(socket.getOutputStream());
		} catch (IOException e) {
			Log.i("PED",e.getMessage());
		}
	}

	public void run() {
		try{
			if(mdis.available()==0)
				sleep(500);
			x = mdis.readInt();
			y = mdis.readInt();
			Log.i("PED",x + "-" +y);
			handler.sendEmptyMessage(TicTacToeServerActivity.RECEIVED_X_Y);
			return;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} 
	}

	public void write(int x, int y) {
		try {
			mdos.writeInt(x);
			mdos.writeInt(y);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

}
